package com.huaboot.system.controller;

import com.huaboot.commont.core.BaseResponse;
import com.huaboot.commont.core.ResultUtils;
import com.huaboot.commont.exception.ErrorCode;
import com.huaboot.commont.page.PageResult;
import com.huaboot.framework.log.annotations.OperateLog;
import com.huaboot.framework.log.enums.OperateTypeEnum;
import com.huaboot.framework.security.user.LoginUser;
import com.huaboot.framework.security.utils.SecurityUtil;
import com.huaboot.system.convert.SysUserConvert;
import com.huaboot.system.query.SysUserQuery;
import com.huaboot.system.service.SysUserService;
import com.huaboot.system.vo.SysUserPasswordVO;
import com.huaboot.system.vo.SysUserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @Author ff
 * @Date 2023/8/27 2:10 下午
 * @Version 1.0
 */
@RestController
@RequestMapping("sys/user")
@AllArgsConstructor
@Tag(name = "用户管理")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('sys:user:page')")
    public BaseResponse<PageResult<SysUserVO>> page(@ParameterObject @Valid SysUserQuery query) {
        PageResult<SysUserVO> page = sysUserService.page(query);
        return ResultUtils.success(page);
    }


    @Operation(summary = "新增用户")
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('sys:user:save')")
    public BaseResponse<?> save(@RequestBody @Valid SysUserVO vo) {
        // 保存
        sysUserService.save(vo);
        return ResultUtils.success();
    }
    @GetMapping("info")
    @Operation(summary = "获取登录用户")
    public BaseResponse<SysUserVO> info() {
        LoginUser authentication = SecurityUtil.getAuthentication();
        SysUserVO convert = SysUserConvert.INSTANCE.convert(authentication);
        assert authentication != null;
        convert.setId(authentication.getUserId());
        return ResultUtils.success(convert);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:user:info')")
    public BaseResponse<SysUserVO> get(@PathVariable("id") Long id) {
        SysUserVO vo = sysUserService.getUserInfo(id);
        return ResultUtils.success(vo);
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('sys:user:delete')")
    public BaseResponse<?> delete(@RequestBody List<Long> idList) {
        Long userId = SecurityUtil.getUserId();
        if (idList.contains(userId)) {
            return ResultUtils.error(ErrorCode.INTERNAL_SERVER_ERROR,"不能删除当前登录用户");
        }
        sysUserService.delete(idList);
        return ResultUtils.success();
    }
    @PutMapping("password")
    @Operation(summary = "修改密码")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    public BaseResponse<?> password(@RequestBody @Valid SysUserPasswordVO vo) {
        sysUserService.updatePassword(vo);
        return ResultUtils.success();
    }
    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:user:update')")
    public BaseResponse<?> update(@RequestBody @Valid SysUserVO vo) {
        sysUserService.update(vo);
        return ResultUtils.success();
    }

}
